#!/usr/local/bin/php
<?php
function fncUpWhois($id, $domain, $tbl) {
    global $dbh;

    // whois.php
    $whois = new Whois();
    $look  = $whois->Lookup($domain);
    $note = json_encode($look);
    $state = 0;
    if ($look['regrinfo']['registered'] == "unknown") {
        return FALSE;
    } elseif ($look['regrinfo']['registered'] == "no") {
        $state = -1;
        if (isset($look['regrinfo']['domain']['status'])) {
            $state = -2;
        }
    } elseif ($look['regrinfo']['registered'] == "yes") {
        $state = 1;
    } else {
        return FALSE;
    }

    // update
    $sql = "UPDATE $tbl SET state=?, note=?, modified=NOW() WHERE id = ?";
    $sth = $dbh->prepare($sql);
    $sth->execute(array($state, $note, $id));
    return TRUE;
}

date_default_timezone_set('Asia/Tokyo');
$cron_start = time();
chdir(dirname(__FILE__));

// ファイルロックによる排他制御
$lockfile = basename(__FILE__, ".php") . ".lock";
$fp = fopen($lockfile, 'w');
if (!flock($fp, LOCK_EX | LOCK_NB)) {   // ロック中
    die('ロック中: ' . $lockfile);
}

$ini = parse_ini_file("whois.ini");     // 設定ファイル
$path = $ini['whois_path'];
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
include('whois.main.php');

// DB接続
$dsn = "mysql:dbname={$ini['db']};host={$ini['host']}";
$user = $ini['user'];
$password = $ini['pass'];
try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    die ('Connection failed: ' . $e->getMessage());
}

// SQL文
$sql_fmt = "SELECT * FROM %s WHERE (modified < DATE_FORMAT(CURDATE(), '%%Y-%%m-01') OR state=0) AND tld %s 'org' ORDER BY modified LIMIT %d";
// SQL実行
$tbls = array('whois1', 'whois3');              // テーブル名
foreach ($tbls as $tbl1) {
    $sql = sprintf($sql_fmt, $tbl1, '<>', 1000);
    $sth1 = $dbh->query($sql);
    if ($row1 = $sth1->fetch(PDO::FETCH_ASSOC)) {
        break;
    }
    $sth1->closeCursor();
}
foreach ($tbls as $tbl2) {
    $sql = sprintf($sql_fmt, $tbl2, '=', 120);
    $sth2 = $dbh->query($sql);
    if ($row2 = $sth2->fetch(PDO::FETCH_ASSOC)) {
        break;
    }
    $sth2->closeCursor();
}

// 結果の利用
$cron_end = $cron_start + (25 * 60);    // cron30分毎想定
$org_wait = 60;
$time_start = 0;
$cnt  = 0;
while ($row1 || row2) {
    // org
    for ($i=0; $i < 4 && $row2; $i++) { // org 1度に4件まで
        $id     = $row2['id'];
        $domain = $row2['domain']. "." .$row2['tld'];
        if (fncUpWhois($id, $domain, $tbl2) !== FALSE) {
            $row2 = $sth2->fetch(PDO::FETCH_ASSOC);
        } else {
            $row2 = FALSE;
        }
    }
    $time_start = $time_end = time();
    if ($cron_end <= $time_end) {
        break;
    }
    $time = $org_wait;
    // org以外
    while ($row1) {
        $id     = $row1['id'];
        $domain = $row1['domain']. "." .$row1['tld'];
        fncUpWhois($id, $domain, $tbl1);
        $time_end = time();
        if ($cron_end <= $time_end) {
            break 2;
        }

        $row1 = $sth1->fetch(PDO::FETCH_ASSOC);
        $time = $org_wait - ($time_end - $time_start);
        if ($time < 0) {
            break;
        }
        $cnt++;
        if ($cnt % 10 == 0) {
            sleep(1);               // 1秒
        }
    }
    if ($row2 && $time > 0) {
        sleep($time);
    }
}

fclose($fp);                // ロックファイルクローズ
?>
